#include "NeoLoadCirclePrivate.h"
#include "NeoLoadCircle.h"
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>

NeoLoadCirclePrivate::NeoLoadCirclePrivate(QObject *parent)
    : QObject(parent),
      pRotateAngle(0),
      pCircleLength(10),
      pBackgroundColor(Qt::gray),
      pTrackColor(Qt::blue),
      pRotateAnimation(new QPropertyAnimation(this, "pRotateAngle", this)),
      pCircleLengthAnimation(new QPropertyAnimation(this, "pCircleLength", this)),
      pAnimationGroup(new QParallelAnimationGroup(this))
{
    NEO_PRIVATE_POINT(NeoLoadCircle)
    pRotateAnimation->setDuration(1000);
    pRotateAnimation->setStartValue(0);
    pRotateAnimation->setEndValue(360);
    pRotateAnimation->setEasingCurve(QEasingCurve::Linear);
    pCircleLengthAnimation->setDuration(1000);
    pCircleLengthAnimation->setStartValue(10);
    pCircleLengthAnimation->setKeyValueAt(0.5, 150);
    pCircleLengthAnimation->setEndValue(10);
    pCircleLengthAnimation->setEasingCurve(QEasingCurve::Linear);
    pAnimationGroup->addAnimation(pRotateAnimation);
    pAnimationGroup->addAnimation(pCircleLengthAnimation);
    pAnimationGroup->setLoopCount(-1);
}

NeoLoadCirclePrivate::~NeoLoadCirclePrivate() = default;

void NeoLoadCirclePrivate::drawBackground(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoLoadCircle)
    painter->save();
    painter->setPen(QPen(pBackgroundColor, pCircleGap));
    painter->drawArc(q->rect().adjusted(NEO_RADIUS_ADJUSTED_VALUES).adjusted(pCircleGap / 2, pCircleGap / 2, -pCircleGap / 2, -pCircleGap / 2), 0, 360 * 16);
    painter->restore();
}

void NeoLoadCirclePrivate::drawTrack(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoLoadCircle)
    painter->save();
    painter->setPen(QPen(pTrackColor, pCircleGap));
    painter->drawArc(q->rect().adjusted(NEO_RADIUS_ADJUSTED_VALUES).adjusted(pCircleGap / 2, pCircleGap / 2, -pCircleGap / 2, -pCircleGap / 2), (pRotateAngle - pCircleLength / 2) * 16, pCircleLength * 16);
    painter->restore();
}